//
//  MineMessageTableViewCell.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import TYAttributedLabel

class MineMessageTableViewCell: UITableViewCell {
    
    typealias ExpandClosure = () -> Void
    
    var expandClosure:ExpandClosure?
    
    var expanded:Bool? {
        didSet{
            // 收起操作
            if !expanded! {
                systemViewHeight.constant = 19
            }else{
                // 展开操作
                let h:CGFloat = getTextHeigh(textStr: messageLabel.text!, font: UIFont.systemFont(ofSize: 14.0), width: messageLabel.mj_w)
                systemViewHeight.constant = h + 5
            }
            showMoreBtn.isSelected = expanded!
        }
    }
    
    private var cellType: MineMessageType = .comment {
        willSet {
            replyButton.isHidden = true
            reCommentLabel.isHidden = newValue == .system
            systemTipView.isHidden = newValue != .system
            let reUILayoutPriority: UILayoutPriority = UILayoutPriority(rawValue: UILayoutPriority.RawValue(newValue == .system ? 251 : 750))
            let systemUILayoutPriority: UILayoutPriority = UILayoutPriority(rawValue: UILayoutPriority.RawValue(newValue == .system ? 750 : 251))
            var contentText = ""
            switch newValue {
            case .comment:
                showMoreBtn.isHidden = true
                break
            case .zan:
                contentText = "赞了"
                showMoreBtn.isHidden = true
                break
            case .system:
                contentText = (self.system?.title)!
                showMoreBtn.isHidden = false
                break
            }
            contentLabel.text = contentText
            reCommentBottomWithSuper.priority = .init(reUILayoutPriority.rawValue)
            systemTipBottomWithSuper.priority = .init(systemUILayoutPriority.rawValue)
        }
    }
    @IBOutlet weak var commendTimeLabel: UILabel!
    @IBOutlet weak var headerImageView: BaseCircleImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var nameLabelWithHeaderImageView: NSLayoutConstraint!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var reCommentLabel: TYAttributedLabel!
    @IBOutlet weak var reCommentLabelHeight: NSLayoutConstraint!
    @IBOutlet weak var reCommentBottomWithSuper: NSLayoutConstraint!
    
    @IBOutlet weak var systemTipView: UIView!
    @IBOutlet weak var systemTipBottomWithSuper: NSLayoutConstraint!
    @IBOutlet weak var replyButton: UIButton!
    
    @IBOutlet weak var systemViewHeight: NSLayoutConstraint!
    @IBOutlet weak var showMoreBtn: UIButton!
    @IBOutlet weak var messageLabel: UILabel!
    var model: myCommendModel?
    var like: myLikeModel?
    var system: systemMessageModel?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func showDataWithComment(model: myCommendModel) {
        self.model = model
        nameLabel.text = model.commentUser?.nickname
        commendTimeLabel.text = model.created
        cellType = .comment
        headerImageView.sd_setImage(with: appendUrl(suffix: (model.commentUser?.headIcon)!), completed: nil)
        contentLabel.text = model.commentContent
        contentLabel.sizeToFit()
       comm()
    }
    
    func showDataWithLike(model: myLikeModel) {
        self.like = model
        nameLabel.text = model.liker?.nickname
        commendTimeLabel.text = model.created
        let headIcon : String = (model.liker?.headIcon)!
        let path:String = "\(BaseURL)\(headIcon)"
        let url = URL(string: path)
        headerImageView.sd_setImage(with: url, completed: nil)
        
        contentLabel.text = model.info?.commentContent
        contentLabel.sizeToFit()
        cellType = .zan
        commlike()
    }
    
    func showDataWithSystem(model: systemMessageModel) {
        self.system = model
        nameLabel.text = model.miUser?.nickname
        commendTimeLabel.text = model.sendTime
        headerImageView.sd_setImage(with: appendUrl(suffix: (model.miUser?.headIcon)!), completed: nil)
        messageLabel.text = model.message
        cellType = .system
    }
    
    func comm() {
        reCommentLabel.setAttributedText(NSAttributedString(string: (self.model?.info?.nickname)!, attributes: [NSAttributedStringKey.foregroundColor : UIColor(hex: 0x00A0E9), NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        reCommentLabel.appendTextAttributedString(NSAttributedString(string: " 发表 ", attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        
        reCommentLabel.appendTextAttributedString(NSAttributedString(string: (self.model?.info?.title)!, attributes: [NSAttributedStringKey.foregroundColor : UIColor(hex: 0x00A0E9), NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        
        reCommentLabelHeight.constant = CGFloat(reCommentLabel.getHeightWithWidth(reCommentLabel.width))
    }
    
    func commlike() {
        reCommentLabel.setAttributedText(NSAttributedString(string: (self.like?.info?.commentator)!, attributes: [NSAttributedStringKey.foregroundColor : UIColor(hex: 0x00A0E9), NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        reCommentLabel.appendTextAttributedString(NSAttributedString(string: " 发表 ", attributes: [NSAttributedStringKey.foregroundColor : UIColor.lightGray, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        
        reCommentLabel.appendTextAttributedString(NSAttributedString(string: (self.like?.info?.commentContent)!, attributes: [NSAttributedStringKey.foregroundColor : UIColor(hex: 0x00A0E9), NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)]))
        
        reCommentLabelHeight.constant = CGFloat(reCommentLabel.getHeightWithWidth(reCommentLabel.width))
    }
    
    @IBAction func replyButtonTap(_ sender: UIButton) {
        
    }
    
    @IBAction func showMoreAction(_ sender: UIButton) {
        if expandClosure != nil {
            expandClosure!()
        }
    }
    
    func getTextHeigh(textStr:String,font:UIFont,width:CGFloat) -> CGFloat {
        
        let normalText: NSString = textStr as NSString
        let size = CGSize.init(width: width, height: 1000)
        let dic = NSDictionary(object: font, forKey: NSAttributedStringKey.font as NSCopying)
        let stringSize = normalText.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic as? [NSAttributedStringKey : Any], context:nil).size
        return stringSize.height
    }
}
